import { _decorator, assetManager, Component, director, JsonAsset, Node } from 'cc';
import ResManager from './common/manager/ResManager';
import { GameData_Ins } from './game/data/GameData';
import { Tables } from './Data/Types';
import AudioManager from './common/manager/AudioManager';
import { MultiPlatform } from './Platform/MultiPlatform';
const { ccclass, property } = _decorator;

@ccclass('StartScene')
export class StartScene extends Component {
  protected onLoad(): void {
    MultiPlatform.Ins().platformSelect();
    AudioManager.Ins.init();
    this.loadTables();
  }

  loadTables() {
    const arr = assetManager.resources.getDirWithPath('data');
    const loadRes = [];
    for (let i = 0; i < arr.length; i++) {
      loadRes.push(arr[i].path);
    }
    ResManager.Ins.loadDir(
      'resources',
      'data',
      (res) => {
        GameData_Ins.tableJson.push(...res);
        this.initTable();
      },
      JsonAsset
    );
    // LoadingManager.Ins().loadResByBundle(LoadingBundle.BaseBundle, loadRes, this.onProgress.bind(this));
  }

  initTable() {
    GameData_Ins.table = new Tables(this.loadTable);
    this.onComplete();
  }

  loadTable(jsonName: string) {
    const jsonAsset = GameData_Ins.tableJson.filter((j: JsonAsset) => j.name === jsonName);
    return jsonAsset.length > 0 ? jsonAsset[0].json : null;
  }

  onComplete() {
    director.loadScene('GameScene');
  }

  start() {
    // assetManager.loadBundle('FindRoadScript', () => {
    //   director.loadScene('FindRoad');
    // });
  }
}
